package org.teamh.gui.login;

import org.teamh.gui.GUI;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Button;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.screen.KeyInputHandler;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class LoginController implements ScreenController{
	
	private GUI gui;
	private Screen screen;
	private ConLostPopUp clPopup;
	
	public LoginController(GUI gui) {
		this.gui = gui;
	}

	@Override
	public void bind(Nifty nifty, Screen screen) {
		this.screen = screen;
	}

	@Override
	public void onEndScreen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStartScreen() {
		
		reactOnEnter("eingabePasswort");
		reactOnEnter("eingabeName");
		
		if(gui.getStart().getConnection().isOnline()) {
			screen.findNiftyControl("loginButton", Button.class).enable();
			screen.findNiftyControl("eingabeName", TextField.class).enable();
			screen.findNiftyControl("eingabePasswort", TextField.class).enable();
		} else {
			screen.findNiftyControl("loginButton", Button.class).disable();
			screen.findNiftyControl("eingabeName", TextField.class).disable();
			screen.findNiftyControl("eingabePasswort", TextField.class).disable();
		}
	}
	
	public void login() {
		gui.getLoginScreen().getBackGroundTimer().cancel();
		String eingabe = screen.findNiftyControl("eingabeName", TextField.class).getText();
		String passwort = screen.findNiftyControl("eingabePasswort", TextField.class).getText();
		
		screen.findNiftyControl("eingabeName", TextField.class).setText("");
		screen.findNiftyControl("eingabePasswort", TextField.class).setText("");
		
		if((eingabe.isEmpty())&&(passwort.isEmpty())) {
			gui.getStart().getConnection().sendMessage("login" + " " + "x" + " " + "x");
		} else {
			gui.getStart().getConnection().sendMessage("login" + " " + eingabe + " " + passwort);

		}		
		changeVisibility("LoginNameTextfieldEffect","LoginPasswordTextfieldEffect");//das erste Argument wird sichtbar, das zweite unsichtbar
		//hideAllTexfieldEffects();//Unsichtbarmachen aller Textfeld-Effekte		

		}			

	
	
	public void exit() {
		gui.getLoginScreen().getBackGroundTimer().cancel();
		gui.getStart().exit();
		changeVisibility("LoginNameTextfieldEffect","LoginPasswordTextfieldEffect");//das erste Argument wird sichtbar, das zweite unsichtbar
		//hideAllTexfieldEffects();//Unsichtbarmachen aller Textfeld-Effekte
	}
	
	private void reactOnEnter(String elementName) {
		screen.findElementByName(elementName).addInputHandler(
				new KeyInputHandler() {
					@Override
					public boolean keyEvent(NiftyInputEvent inputEvent) {
						if (inputEvent == null)
							return false;
						switch (inputEvent) {
						case SubmitText:
							login();
							return true;
						default:
							break;
						}
						return false;
					}
				});

	}
	
	public void switchToEditor() {
//		System.out.println("----------------------------------");
//		//screen.findNiftyControl("mapEditorButton", Button.class).setFont(gui.getNifty().getRenderEngine().getRenderDevice().createFont("res/font/verdana.ttf"));
//		RenderFont font = gui.getNifty().getRenderEngine().getRenderDevice().createFont("verdana-48-regular.fnt");
//		
//		//screen.findNiftyControl("mapEditorButton", Button.class).setFont(font);
//		//screen.findNiftyControl("eingabeName", Button.class).setFont(font);
//		
//		gui.getNifty().getRenderEngine().setFont(gui.getNifty().getRenderEngine().getRenderDevice().createFont("verdana-48-regular.fnt"));
//		
//		//screen.findNiftyControl("test", ListBox.class).
//		System.out.println("----------------------------------");
		
//		screen.findNiftyControl("eingabeName", TextField.class).setText("ricpet");
//		screen.findNiftyControl("eingabePasswort", TextField.class).setText("schrott1");
//		login();
		
		gui.setFollowingScreen(gui.getMapEditorScreen());
		changeVisibility("LoginNameTextfieldEffect","LoginPasswordTextfieldEffect");//das erste Argument wird sichtbar, das zweite unsichtbar
		//hideAllTexfieldEffects();//Unsichtbarmachen aller Textfeld-Effekte
	}
	
	public void changeVisibility(String showname,String hidename) {
		show(showname);
		hide(hidename);
	}
	
	public void hideAllTexfieldEffects() {
		hide("LoginNameTextfieldEffect");
		hide("LoginPasswordTextfieldEffect");
	}
	public void hide(String hidename) {
		screen.findElementByName(hidename).hide();
	}
	public void show(String showname) {
		screen.findElementByName(showname).show();
	}
	
	public void showErrorPopup() {
		clPopup = new ConLostPopUp(gui);
		gui.getNifty().showPopup(gui.getNifty().getScreen("loginScreen"), clPopup.getErrorPopup().getId(), null);
	}
	
	public void closeErrorPopup() {
		gui.getNifty().closePopup(clPopup.getErrorPopup().getId());
	}
}
